
import 'dart:convert';
import 'dart:io';

import 'package:fish_redux/fish_redux.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:image_picker/image_picker.dart';
import 'package:oktoast/oktoast.dart';
import 'package:school_safe_teacher/base/base_entity.dart';
import 'package:school_safe_teacher/base/entity_factory.dart';
import 'package:school_safe_teacher/constans/all_info.dart';
import 'package:school_safe_teacher/me/entity/userEntity.dart';
import 'package:school_safe_teacher/network/dio_api.dart';
import 'package:school_safe_teacher/network/http_api.dart';
import 'package:school_safe_teacher/user_info/upload_entity.dart';

import 'action.dart';
import 'change_nickname/action.dart';
import 'change_user_info/action.dart';
import 'state.dart';

Effect<UserInfoState> buildEffect() {
  return combineEffects(<Object, Effect<UserInfoState>>{
    Lifecycle.initState: _init,
    UserInfoAction.chooseSex: _chooseSex,
    ChangeNickNameAction.changeNickNameSuccess:_init,
    ChangeUserInfoAction.changeInfoSuccess:_init,
    UserInfoAction.choosePic: _onChoosePic,
    UserInfoAction.cameraPic: _onCameraPic,
  });
}

void _init(Action action, Context<UserInfoState> ctx) {
  if (AllInfo.instance.token.isNotEmpty) {
    UserEntity userInfoEntity = AllInfo.instance.userEntity;
    ctx.state
      ..userInfoEntity = userInfoEntity
      ..sexType = userInfoEntity.sex.toString()
      ..sex = userInfoEntity.sex == 1 ? "男" : "女";
    ctx.dispatch(UserInfoActionCreator.onAction());
  }
}

void _chooseSex(Action action, Context<UserInfoState> ctx) {
  ctx.state..sexType = action.payload;
  if (ctx.state.sexType == "1") {
    ctx.state..sex = "男";
  } else {
    ctx.state..sex = "女";
  }
  Map<String, String> params = Map();
  params["field"] = "sex";
  params["value"] = ctx.state.sexType;
  DioApi.instance.asyncRequestNetwork<BaseEntity>(
      Method.post, HttpApi.EDIT_USER_INFO,ctx.context,
      params: params, onStart: () {
    println('请求开始');
    EasyLoading.show();
  }, onSuccess: (dynamic t) {
    ctx.state.userInfoEntity..sex = int.parse(ctx.state.sexType);
    AllInfo.instance.setUserEntity(ctx.state.userInfoEntity);
    ctx.dispatch(UserInfoActionCreator.onAction());
    showToast("修改成功");
  }, onError: (String msg) {
    println('请求报错 msg:$msg');
    showToast(msg);
  }, onDone: () {
    EasyLoading.dismiss();
  }, isList: false);
}

Future<void> _onCameraPic(
    Action action, Context<UserInfoState> ctx) async {
  PickedFile pickedFile =
  await ctx.state.picker.getImage(source: ImageSource.camera);
  _uploadPic(pickedFile, ctx);
}

Future<void> _onChoosePic(
    Action action, Context<UserInfoState> ctx) async {
  PickedFile pickedFile =
  await ctx.state.picker.getImage(source: ImageSource.gallery);
  _uploadPic(pickedFile, ctx);
}

Future<void> _uploadPic(
    PickedFile pickedFile, Context<UserInfoState> ctx) async {
  File file = File(pickedFile.path);
  List<int> imageBytes = await file.readAsBytes();
  String base64 = base64Encode(imageBytes);
  print(base64);
  Map<String, String> params = Map();
  params["image"] = base64;
  DioApi.instance.asyncRequestNetwork<dynamic>(
      Method.post, HttpApi.UP_LOAD_IMAGE,ctx.context,
      params: params, onStart: () {
    println('请求开始');
    EasyLoading.show();
  }, onSuccess: (dynamic t) async {
    UploadEntity entity = EntityFactory.generateOBJ(t);
    Map<String, String> params = Map();
    params["field"] = "headimg";
    params["value"] = entity.path;
    DioApi.instance.asyncRequestNetwork<BaseEntity>(
        Method.post, HttpApi.EDIT_USER_INFO,ctx.context,
        params: params, onStart: () {
      println('请求开始');
      EasyLoading.show();
    }, onSuccess: (dynamic t) {
      ctx.state.userInfoEntity..headimg = entity.path;
      AllInfo.instance.setUserEntity(ctx.state.userInfoEntity);
      showToast("修改成功");
      ctx.dispatch(UserInfoActionCreator.onAction());
    }, onError: (String msg) {
      println('请求报错 msg:$msg');
      showToast(msg);
    }, onDone: () {
      EasyLoading.dismiss();
    }, isList: false);
  }, onError: (String msg) {
    println('请求报错 msg:$msg');
    showToast(msg);
  }, onDone: () {
    EasyLoading.dismiss();
  }, isList: false);
}
